<template>
  <div class="dashboard-container">
    <div class="app-container">
      <upload-excel :before-upload="beforeUpload" :on-success="successFn" />
    </div>
  </div>
</template>

<script>
import employeesEnum from '@/constant/employees'
import { importEmployees } from '@/api/employees'
import { parseTime } from '@/utils'
const { mapKeyPath } = employeesEnum
export default {
  methods: {
    beforeUpload(file) {
      if (file.name.endsWith('.xlsx')) {
        return true
      }
      return alert('请上传excel文件')
    },
    async successFn({ header, results }) {
      console.log(header, results)
      // const data = results.map(item => {
      //   return {
      //     correctionTime: item.转正日期,
      //     mobile: item.手机号,
      //     timeOfEntry: item.入职日期,
      //     username: item.姓名,
      //     workNumber: item.工号
      //   }
      // })
      const res = results.map(item => {
        const obj = {}
        for (const key in item) {
          if (key === '入职日期' || key === '转正日期') {
            const timeStamp = item[key]
            // 先减去一天 再把天数转化为毫秒
            const date = new Date((timeStamp - 1) * 24 * 3600000)
            // 把得到的时间减去70年
            const time = parseTime(date.setFullYear(date.getFullYear() - 70), '{y}-{m}-{d}')
            obj[mapKeyPath[key]] = time
          } else { obj[mapKeyPath[key]] = item[key] }
        }
        return obj
      })
      console.log(res)
      await importEmployees(res)
      this.$message.success('导入成功')
      this.$router.go(-1)
    }
  }
}
</script>

<style>

</style>
